package com.jwsoft.manager.core.integration.common.impl;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.enums.AccountType;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.EquipmentTypeEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.module.base.common.enums.UserAddTypeEnum;
import com.bifang.module.base.common.vo.common.CommonVO;
import com.bifang.module.base.common.vo.system.LoginVO;
import com.bifang.module.base.common.vo.system.TokenVO;
import com.bifang.module.base.core.integration.CommonIntegration;
import com.bifang.module.base.core.integration.IndexIntegration;
import com.bifang.module.system.common.enums.CardTypeEnum;
import com.bifang.module.system.common.enums.UserTypeEnum;
import com.jwsoft.manager.common.vo.common.XzdyAppVO;
import com.jwsoft.manager.core.integration.common.XzdyLoginIntegration;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;
@ApiService(funcCode = "xzdy", title = "学在东阳")
@Slf4j
public class XzdyLoginIntegrationImpl implements XzdyLoginIntegration {
    @Autowired
    CommonIntegration commonIntegration;
    @Autowired
    IndexIntegration indexIntegration;
    @Override
    @OpApi(funcCode = "xzdy0001", title = "学在东阳浙里办端登录", funcType = FuncTypeEnum.query,
            checkPrivilege = BoolEnum.FALSE,
            publicFlag = BoolEnum.TRUE)
    public TokenVO loginApp(XzdyAppVO vo) {
        if(StringUtils.isBlank(vo.getZlbUserId())){
            throw new AppException(1,"浙里办用户id不能为空");
        }
        CommonVO urlVO = new CommonVO();
        urlVO.setParamKey("edu_xzdy_login_url");
        urlVO.setAreaCode("330783");
        String url = commonIntegration.getValueByKeyAndArea(urlVO);
        if (StringUtils.isBlank(url)) {
            throw new AppException(1,"请先在系统中配置学在东阳信息认证地址：edu_xzdy_login_url");
        }
        CommonVO reqKeyVO = new CommonVO();
        reqKeyVO.setParamKey("edu_xzdy_auth_requestKey");
        reqKeyVO.setAreaCode("330783");
        String requestKey = commonIntegration.getValueByKeyAndArea(reqKeyVO);
        if (StringUtils.isBlank(requestKey)) {
            throw new AppException(1,"请先在系统中配置学在东阳信息认证KEY：edu_xzdy_auth_requestKey");
        }
        Map<String,Object> map=new HashMap<>();
        map.put("requestKey",requestKey);
        map.put("zlbUserId",vo.getZlbUserId());
        String params= JSON.toJSONString(map);
        url=url+"?requestKey="+requestKey+"&zlbUserId="+vo.getZlbUserId();
        String body = HttpUtil.post(url, params, 3000);
        log.info("学在东阳用户信息："+body);
        if (StringUtils.isBlank(body)) {
            throw new AppException(1,"登录失败，学在东阳未返回信息");
        }
        JSONObject jsonObject = null;
        try {
            jsonObject = JSONObject.parseObject(body);
        } catch (Exception e) {
            throw new AppException(1,"登录失败：" + body);
        }
        String code=jsonObject.getString("resultCode");
        if(StringUtils.isBlank(code)){
            code=jsonObject.getString("code");
        }
        String msg=jsonObject.getString("msg");
        if(StringUtils.isBlank(msg)){
            msg=jsonObject.getString("result");
        }
        if (StringUtils.isBlank(code)||!"01".equalsIgnoreCase(code)){
            throw new AppException(1,"学在东阳返回失败code("+code+"):"+msg);
        }
        JSONObject data=jsonObject.getJSONObject("data");
        LoginVO loginReqVO =new LoginVO();
        String phone=data.getString("phone");//手机
        String idcard=data.getString("idcard");//身份证
        String cubeUserId=data.getString("cubeUserId");//魔方id
        String userName=data.getString("userName");//姓名
        loginReqVO.setIdcard(idcard);
        loginReqVO.setFullName(userName);
        loginReqVO.setCellphone(phone);
        if(StringUtils.isBlank(vo.getUserType())){
            loginReqVO.setUserType(UserTypeEnum.USER.getType());
        }else{
            loginReqVO.setUserType(vo.getUserType());
        }
        if (StringUtils.isBlank(idcard)){
            throw new AppException(1,"学在东阳返回证件号码为空");
        }
        if (IdcardUtil.isValidCard(idcard)){
            loginReqVO.setCardType(CardTypeEnum.idcard.getType());
        }else{
            loginReqVO.setCardType(CardTypeEnum.other.getType());
        }
        loginReqVO.setAddType(UserAddTypeEnum.userType.getType());
        loginReqVO.setLoginType(AccountType.ZLB.getKey());
        loginReqVO.setEquipmentType(EquipmentTypeEnum.APP.getType());
        loginReqVO.setOpenId(vo.getZlbUserId());//使用浙里办用户id
        loginReqVO.setUid(cubeUserId);
        return indexIntegration.login(loginReqVO);
    }
}
